package com.gitee.tianchaohongyu.jdcl.support;

import com.gitee.tianchaohongyu.jdcl.core.Cleaner;
import com.gitee.tianchaohongyu.jdcl.core.CleanerAnnotation;
import com.gitee.tianchaohongyu.jdcl.util.StringUtils;

import javax.validation.constraints.NotNull;
import java.lang.annotation.*;

/**
 * 将null、"null"、"undefined"视为null
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(value = {ElementType.FIELD, ElementType.PARAMETER, ElementType.TYPE, ElementType.METHOD})
@CleanerAnnotation(UndefinedAsNull.TempCleaner.class)
public @interface UndefinedAsNull {

  /**
   * 清洗器实现
   */
  class TempCleaner implements Cleaner<String, UndefinedAsNull> {

    @Override
    public <R extends String> R clean(R data, @NotNull Class<R> defineType, UndefinedAsNull annotation) {
      return StringUtils.isUndefined(data) ? null : data;
    }
  }
}
